package ee.serge.util;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Widget;

public class AnimPanel extends AbsolutePanel {

   private Animation anim;
   private Widget widget;
   private int maxheight;
   
   public void setWidget(Widget widget) {
      this.widget = widget;
      add(widget);
      setWidgetPosition(widget, 0, 0);
   }
   
   public void run(int height) {
      maxheight = height;
      anim = new Anim(this, height);
      anim.run(100000);
   }
   
   public void setScroll(int height) {
      setHeight(String.valueOf(height) + "px");
      setWidgetPosition(widget, 0, height - maxheight);
   }
   
   class Anim extends Animation {

      private AnimPanel animPanel;
      private int height;
      private long millis;
      private float delta;
      private float full;
      private int maxHeight;

      public Anim(AnimPanel animPanel, int height) {
         this.animPanel = animPanel;
         maxHeight = height;
      }
      
      @Override
      protected void onUpdate(double progress) {
         if (height >= maxHeight) cancel();
         delta = (float)(System.currentTimeMillis() - millis)  / 10;
         millis = System.currentTimeMillis();
         System.out.println("d: " + delta);
         full += delta;
         System.out.println("f: " + full);
         height += (int)full;
         if (height > maxHeight) height = maxHeight;
         full -= (int)full;
         animPanel.setScroll(height);
         System.out.println("f: " + full);
         System.out.println("");
      }

      @Override
      protected void onStart() {
         animPanel.setHeight("0");
         millis = System.currentTimeMillis();
      }
      
   }

}


